package cn.jimmie.learn.viewArt.ball;

import android.app.Activity;

/**
 * function : 对外暴露的 悬浮球管理类
 *
 * <p> Create Time: 2018年12月31日 09:36 </p>
 * <p> @author Jimmie.Qian  </p>
 */
public class AssistBall implements IProxy {
    private final AssistBallProxy mProxy;

    private AssistBall(Builder builder) {
        this.mProxy = new AssistBallProxy(builder);
    }

    public int getBallX() {
        return mProxy.getBallX();
    }

    public int getBallY() {
        return mProxy.getBallY();
    }

    /**
     * 创建悬浮球唯一的方式
     */
    public static Builder creator(Activity activity) {
        return new Builder(activity);
    }

    @Override
    public void attacheToWindow() {
        mProxy.attacheToWindow();
    }

    @Override
    public void detachFromWindow() {
        mProxy.detachFromWindow();
    }

    @Override
    public boolean isAttached() {
        return mProxy.isAttached();
    }

    @Override
    public void addMessage(Message message, OnAssistPopEventListener listener) {
        mProxy.addMessage(message, listener);
    }

    @Override
    public void addMessage(Message message) {
        addMessage(message, null);
    }

    @Override
    public void updatePing(int ping) {
        mProxy.updatePing(ping);
    }

    @Override
    public void setRedDot(boolean visibility) {
        mProxy.setRedDot(visibility);
    }

    @Override
    public void setBallEventListener(OnAssistBallEventListener listener) {
        mProxy.setBallEventListener(listener);
    }

    /**
     * 浮白事件监听器
     */
    public interface OnAssistPopEventListener {
        /**
         * 点击浮白关闭按钮回调
         *
         * @param byUser 是否是用户手动触发
         */
        void onPopCloseClick(boolean byUser);

        /**
         * 点击下一条的回调
         *
         * @param byUser 是否是用户手动触发
         */
        void onPopNextClick(boolean byUser);

        /**
         * 点击链接的回调
         */
        void onPopContentClick();
    }

    /**
     * 悬浮球事件监听器
     */
    public interface OnAssistBallEventListener {
        /**
         * 浮球点击的回调
         *
         * @param lock 浮球是否处于锁定状态
         */
        void onBallClick(boolean lock);

        /**
         * 浮球长按的回调(锁定和不锁定都可响应)
         *
         * @param lock 浮球是否处于锁定状态
         */
        void onBallLongClick(boolean lock);

        /**
         * 浮球添加到windows上的回调
         */
        void onBallAttached();

        /**
         * 浮球消失的回调
         */
        void onBallDetached();

        /**
         * 浮球贴边时回调
         */
        void onBallAttachedEdge();

        /**
         * 浮球拖动时回调
         */
        void onBallActionMove(int x, int y);
    }

    /**
     * 浮白消息
     */
    public static class Message {
        final String content;
        final String title;
        final boolean enableContentClick;
        final boolean isIndiePop;
        String[] contentLines;
        float contentHeight;
        float titleHeight;
        float textUnit;

        Message(MessageBuilder builder) {
            this.title = builder.title;
            this.content = builder.content;
            this.enableContentClick = builder.enableContentClick;
            this.isIndiePop = builder.unMulti;
        }
    }

    /**
     * 对外提供的悬浮球创建类
     */
    public static class Builder {
        final Activity activity;
        int logoStyle = 0;
        int position = 0;
        int edgeInNormalWarning;
        int edgeInWarningDanger;

        Builder(Activity activity) {
            this.activity = activity;
        }

        /**
         * 设置悬浮球的样式类别
         *
         * @param logoStyle 提供四种样式, 取值从 0~3
         * @return self
         */
        public Builder setStyle(int logoStyle) {
            this.logoStyle = logoStyle;
            return this;
        }

        /**
         * 设置悬浮球初始化的位置
         *
         * @param position 四个方位 0:LEFT;1:BOTTOM;2:RIGHT;3:TOP
         * @return self
         */
        public Builder setPosition(int position) {
            this.position = position;
            return this;
        }

        /**
         * 设置ping值边界
         */
        public Builder setPingEdges(int edge1, int edge2) {
            edgeInNormalWarning = edge1;
            edgeInWarningDanger = edge2;
            return this;
        }

        public AssistBall create() {
            return new AssistBall(this);
        }
    }

    public static class MessageBuilder {
        String title = "";
        String content = "";
        boolean enableContentClick = false;
        boolean unMulti = true;

        /**
         * 设置Message的标题
         */
        public MessageBuilder title(String title) {
            this.title = title == null ? "" : title;
            return this;
        }

        /**
         * 设置Message的内容
         */
        public MessageBuilder content(String content) {
            this.content = content == null ? "" : content;
            return this;
        }

        /**
         * Message是否支持 链接响应
         */
        public MessageBuilder enableMessageClick(boolean enableContentClick) {
            this.enableContentClick = enableContentClick;
            return this;
        }

        /**
         * 是否是独立的窗口, false:表示独立窗口,不会显示下一条
         */
        public MessageBuilder enableMulti(boolean multi) {
            this.unMulti = !multi;
            return this;
        }

        public Message create() {
            return new Message(this);
        }
    }
}
